library(readstata13) # package to read Stata 13 dta files
library(tidyverse)
library(lubridate) # Working with dates
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = T)
set.seed(1)

stan_data <- readRDS("Results/stan_data_for_fit_2021_01_18_second_round.rds")

system.time(m <- stan_model(file = "Scripts/Poisson_model_lasso.stan"))

args <- commandArgs(trailingOnly = TRUE)


if (length(args) < 1) {
  vb_fit <- vb(object = m, data = stan_data, iter = 30000)
} else {
  vb_fit <- vb(object = m, data = stan_data, iter = 30000, seed = args[1])
}

filename <- "Results/vb_estimated_shrunk_hazard_rate_2021_01_18_second_round"

if (length(args) > 1) {
  filename <- paste(filename, args[2], sep = "_")
}


print(paste("saving to", filename))

res = list(vb_fit = vb_fit, data = stan_data)

saveRDS(res, file = paste(filename, "rds", sep = "."))
